
//服务器返回消息处理
//根据 服务器 返回的不同状态 去执行相对应的事件
this.addEventListener = function (ws) {
    ws.onmessage = function (evt) {
        console.info('event_date', evt);
        var evtVar = evt.data.replace('\\r\\n', '').replace('"{', '{').replace('}"', '}');
        if (evtVar.substr(5, 1) === '9') {
            var jsonStr = JSON.parse(evtVar.substr(33, evtVar.length));
            console.log("event:", jsonStr.event);
            if (jsonStr.event === "OnAgentLogonEvent") {//签入成功事件
                top.softphone.first = true;
                top.softphone.currAgentStatus = '签入';
                top.softphone.agentStateChangeBtnUpdate('Login', jsonStr);
                top.softphone.updateAgentStatus('Login', '签入');
                top.softphone.updateAgentStatusLog('Logout', '签入', '0');
            } else if (jsonStr.event === "OnAgentLogoffEvent") {//签出成功事件
                top.softphone.agentStateChangeBtnUpdate('Logout', jsonStr);
                top.softphone.updateAgentStatus('Logout', '签出');
                if (top.softphone.currAgentStatus === '就绪') {
                    top.softphone.updateAgentStatusLog('Ready', '就绪', '2');
                } else if (top.softphone.currAgentStatus === '未就绪') {
                    top.softphone.updateAgentStatusLog('NotReady', '未就绪', '3');
                }
                top.softphone.updateAgentStatusLog('Login', '签入', '0');
            } else if (jsonStr.event === "OnCallRingBackEvent") {//外呼目标振铃事件
                top.softphone.currAgentStatus = '外呼振铃';
                let properties = jsonStr.properties;
                //保存随路数据
                top.softphone.agentStateChangeBtnUpdate('MakeCall', jsonStr);
                top.softphone.insertCallHistory('out', jsonStr);//插入呼叫历史
                top.softphone.updateAgentStatus('MakeCall', '外呼');
                top.softphone.saveSessionData('out', properties);
            } else if (jsonStr.event === "OnAgentAvailableEvent") {//就绪成功事件
                top.softphone.currAgentStatus = '就绪';
                top.softphone.agentStateChangeBtnUpdate('Ready', jsonStr);
                top.softphone.updateAgentStatus('Ready', '就绪');
                if (top.softphone.toIvrFlag !== 'checkPwd') {
                    top.softphone.updateAgentStatusLog('NotReady', '未就绪', '0');
                }
            } else if (jsonStr.event === "OnAgentAuxEvent") {//未就绪事件
                top.softphone.currAgentStatus = '未就绪';
                top.softphone.agentStateChangeBtnUpdate('NotReady', jsonStr);
                top.softphone.updateAgentStatus('NotReady', '未就绪');
                if (top.softphone.first) {
                    top.softphone.updateAgentStatusLog('NotReady', '未就绪', '0');
                    top.softphone.first = false;
                } else {
                    if (top.softphone.toIvrFlag !== 'checkPwd') {
                        top.softphone.updateAgentStatusLog('Ready', '就绪', '0');
                    }
                }
            } else if (jsonStr.event === "OnCallReleaseEvent") {//呼叫语音结束事件（话后处理）
                top.softphone.currAgentStatus = '通话结束';
                top.softphone.agentStateChangeBtnUpdate('WorkNotReady', jsonStr);
                top.softphone.updateCallHistory('2', jsonStr);//更新呼叫历史
                top.softphone.updateAgentStatus('WorkNotReady', '未就绪');
                // console.log('通话结束后执行1————————updateCallHistory');
            } else if (jsonStr.event === "OnCallEndedEvent") {//呼叫结束事件
                top.softphone.currAgentStatus = '挂机';
                // top.softphone.agentStateChangeBtnUpdate('WorkNotReady',jsonStr);
                //挂机后自动示忙
                // top.softphone.busyBtnClick();
                // top.softphone.callDropAfter(jsonStr);
                // console.log('通话结束后执行2————————updateCallHistory');
                top.softphone.updateCallHistory('callEnded', jsonStr);//更新呼叫历史
                // top.softphone.updateAgentStatus('WorkNotReady','未就绪');
                // top.softphone.updateAgentStatusLog('WorkNotReady','未就绪','0');
                top.softphone.clearSession('1');
            } else if (jsonStr.event === "OnAvayaIcOperationFailedEvent") {//签入失败事件
                // window.getElementMessage('签入失败，请检查分机是否正常！');
            } else if (jsonStr.event === "OnCallIncommingEvent") {//来电振铃事件
                //保存随路数据
                // window.getSessionObject(properties.uniqueId,properties);
                top.softphone.saveSessionData('in', jsonStr.properties);
                top.softphone.currAgentStatus = '来电振铃';
                top.softphone.agentStateChangeBtnUpdate('CallIncomming', jsonStr);
                top.softphone.insertCallHistory('in', jsonStr);//插入呼叫历史
                top.softphone.updateAgentStatus('CallIncomming', '来电振铃');
                // top.softphone.updateAgentStatusLog('CallIncomming','来电振铃','0');
                if (top.softphone.isAothAnwser()) {//判断是否自动应答
                    console.log("自动应答");
                    top.softphone.answerBtnClick();
                } else {
                    console.log("非自动应答");
                    top.softphone.showErrorInfo('提示', '您有新的来电，请注意接听！', 'CallIncomming');
                }
            } else if (jsonStr.event === "OnCallEstablishedEvent") {//通话事件
                top.softphone.currAgentStatus = '通话中';
                top.softphone.agentStateChangeBtnUpdate('CallEstablished', jsonStr);
                top.softphone.updateAgentStatus('CallEstablished', '通话中');
                // top.softphone.updateAgentStatusLog('CallEstablished','通话中','0');
                top.softphone.updateCallHistory('1', jsonStr);//更新呼叫历史
                // console.log('通话结束后执行3————————updateCallHistory');
            } else if (jsonStr.event === "OnCallHeldEvent") {//保持成功事件
                if (top.softphone.toIvrFlag === 'checkPwd') {
                    top.softphone.agentStateChangeBtnUpdate('checkPwd', jsonStr);
                } else {
                    top.softphone.currAgentStatus = '保持';
                    top.softphone.agentStateChangeBtnUpdate('CallHeld', jsonStr);
                    top.softphone.updateAgentStatus('CallHeld', '保持');
                    // top.softphone.updateAgentStatusLog('CallHeld','保持','0');
                }
            } else if (jsonStr.event === "OnCallRetrievedEvent") {//取消保持成功事件
                if (top.softphone.toIvrFlag === 'checkPwd') {
                    top.softphone.conferenceToIvrStauts('hold');
                }
                top.softphone.currAgentStatus = '通话中';
                top.softphone.agentStateChangeBtnUpdate('Retrieve', jsonStr);
                top.softphone.updateAgentStatus('CallEstablished', '通话中');
                // top.softphone.updateAgentStatusLog('CallEstablished','通话中','0');
            } else if (jsonStr.event === "OnCallAfterCallEvent") {//话后处理事件
                top.softphone.currAgentStatus = '事后处理';
                top.softphone.agentStateChangeBtnUpdate('CallAfterCall', jsonStr);
                top.softphone.callDropAfter(jsonStr);
                // top.softphone.updateCallHistory('2',jsonStr);//更新呼叫历史
                top.softphone.updateAgentStatus('CallAfterCall', '事后处理');
                // top.softphone.updateAgentStatusLog('CallAfterCall','事后处理','0');
            } else if (jsonStr.event === "OnNonCallIncommingEvent") {//存在非活动呼叫事件
                top.softphone.currAgentStatus = '存在非活动呼叫事件';
                console.log("OnNonCallIncommingEvent 存在非活动呼叫事件");
                top.softphone.answerBtnClick();
                top.softphone.hangupBtnClick();
            } else if (jsonStr.event === "OnCollTransferBeginEvent") {//咨询接起事件
                top.softphone.currAgentStatus = '等待咨询接起';
                top.softphone.agentStateChangeBtnUpdate('CollTransferBegin', jsonStr);
                top.softphone.updateAgentStatus('CollTransferBegin', '等待咨询接起');
                // top.softphone.updateAgentStatusLog('CollTransferBegin','等待咨询接起','0');
            } else if (jsonStr.event === "OnDestConnectedEvent") {//会议接起事件
                if (top.softphone.toIvrFlag === 'checkPwd') {
                    top.softphone.conferenceToIvrStauts('conferenceAgentOk');
                    top.softphone.updateCallHistory('ivrBegin', jsonStr);//更新呼叫历史
                    // console.log('通话结束后执行4————————updateCallHistory');
                }
                top.softphone.currAgentStatus = '咨询';
                top.softphone.agentStateChangeBtnUpdate('DestConnected', jsonStr);
                top.softphone.updateAgentStatus('DestConnected', '咨询');
                // top.softphone.updateAgentStatusLog('DestConnected','咨询','0');

            } else if (jsonStr.event === "OnCallPartyDropedEvent") {//会议中某方退出事件
                if (top.softphone.toIvrFlag === 'checkPwd') {
                    top.softphone.conferenceToIvrStauts('retrieve');
                    top.softphone.ivrReturnToAgent('AUTH');
                    top.softphone.toIvrFlag = 'toIvrEnd';
                    top.softphone.updateCallHistory('ivrEnd', jsonStr);//更新呼叫历史
                    // console.log('通话结束后执行5————————updateCallHistory');
                }
                top.softphone.currAgentStatus = '会议中某方退出';
                top.softphone.agentStateChangeBtnUpdate('PartyDroped', jsonStr);
                top.softphone.updateAgentStatus('PartyDroped', '咨询');
                // top.softphone.updateAgentStatusLog('PartyDroped','咨询','0');
            } else if (jsonStr.event === "OnCollConferenceBeginEvent") {//发起会议事件
                top.softphone.currAgentStatus = '发起会议';
                top.softphone.agentStateChangeBtnUpdate('ConferenceBegin', jsonStr);
                top.softphone.updateAgentStatus('ConferenceBegin', '发起会议');
                // top.softphone.updateAgentStatusLog('ConferenceBegin','发起会议','0');
            }
        }
    };
}
